package opf5.ui;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

public class OPF5Tabla extends JScrollPane {
	private static final long serialVersionUID = 1L;

	private JScrollPane scrollPane;
	private DefaultTableModel tableModel;
	public JTable table;
	private String[] columnas;
	
	public OPF5Tabla(String[] columnas) {
		this.columnas = columnas;
		
		tableModel = new DefaultTableModel(null, columnas);
		table = new JTable(tableModel);
		
		table.setPreferredScrollableViewportSize(this.getPreferredSize());
		table.setFillsViewportHeight(true);
		
		table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		
		this.add(table);
		this.setViewportView(table);
	}

	public void agregarFila(String[] fila) {
		tableModel.addRow(fila);
	}

	public void setContenido(String[][] contenido) {
		limpiar();
		for (String[] fila : contenido) {
			tableModel.addRow(fila);
		}
	}
	
	public int getIndice() {
		return table.getSelectedRow();
	}

	public void limpiar() {
		while(tableModel.getRowCount() != 0) {
			this.tableModel.removeRow(0);
		}
	}
}
